package com.yhg.context.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhg.common.utils.R;
import com.yhg.context.entity.BlBlog;
import com.yhg.context.entity.vo.BlogListVo;
import com.yhg.context.feign.AdminFeign;
import com.yhg.context.service.BlBlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 博客表 前端控制器，哈哈哈，嘿嘿嘿
 * </p>
 *
 * @author 白小白
 * @since 2021-02-01
 */
@Api(description = "博客管理")
@RestController
@RequestMapping("/context/blog")
@CrossOrigin
public class BlBlogController {

    @Autowired
    BlBlogService blogService;
    @Autowired
    AdminFeign adminFeign;

    /**
     * 在blog调用member方法,调用member里面的service
     */
    @ApiOperation(value = "远程调用查询admin")
    @GetMapping("blogToMember")
    public R blogToMember(){
        R list = adminFeign.list();
        return R.ok().data("data",list);
    }

    /**
     * 发布博客
     * @author yang
     */
    @ApiOperation(value = "发布博客")
     @PostMapping("saveBlog")
    public R saveBlog(@RequestBody BlBlog blBlog){  //RequestBody请求体
         boolean b = blogService.save(blBlog);
         if (b)  return R.ok();
         else return R.error();

     }

    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询博客" )
    @GetMapping("selectOne/{id}")
    public R seleteOne(@PathVariable String id){
        BlBlog blog = blogService.getById(id);
        return R.ok().data("data",blog);
    }

    /**删除博客
     * @author yang
     */
    @ApiOperation(value = "删除博客")
    @DeleteMapping("delete/{id}")
    public R delete(@ApiParam(name = "id", value = "博客ID", required = true) @PathVariable("id") String id){
        boolean b = blogService.removeById(id);
        return R.ok();
    }

    /**
     * 更新博客
     */
    @ApiOperation(value = "更新博客")
    @PostMapping("updateOne")
    public R updateOne(@RequestBody BlBlog blBlog){
        boolean flag = blogService.updateById(blBlog);//这个类里包含id，传入整个也可以
        if (flag){
            return R.ok();
        }else{
            return R.error();
        }

    }

    /**查询所有博客
     * @author yang
     */
    @ApiOperation(value = "查询所有博客")
    @GetMapping("list")
    public R list(){
        List<BlBlog> list = blogService.list(null);
        return R.ok().data("blList",list).message("查询全部博客成功");
    }

    /**
     * 带条件与分页功能的查询
     */
    @ApiOperation(value = "带条件分页查询")
    @PostMapping("listPage/{current}/{size}")
    public R listPage(@PathVariable("current") Long current,
                      @PathVariable("size") Long size ,
                      @RequestBody(required = false) BlogListVo vo
                      ){
        //new一个分页对象
        Page<BlBlog> page = new Page<>(current,size);
        //new一个条件构造器
        QueryWrapper<BlBlog> wrapper = new QueryWrapper<>();
        //构造条件.if判断
        if (vo == null) {
            blogService.page(page,wrapper);
        }else{
            //构造条件
            if(!StringUtils.isEmpty(vo.getBlogTitle())){
                wrapper.like("blog_title",vo.getBlogTitle());
            }
            if(!StringUtils.isEmpty(vo.getBlogType())){
                wrapper.eq("blog_type",vo.getBlogType());
            }
            if(!StringUtils.isEmpty(vo.getBegin())){
                wrapper.ge("gmt_create",vo.getBegin());
            }
            if(!StringUtils.isEmpty(vo.getEnd())){
                wrapper.le("gmt_create",vo.getEnd());
            }
            blogService.page(page,wrapper);   //条件分页查询
        }
        List<BlBlog> recordsList = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("records",recordsList).data("total",total);
    }

}

